
from typing import List

from trader.event import EVENT_BAR
from vnpy.event import EventEngine, Event
from vnpy.trader.database import BaseDatabase, get_database, TickOverview
from vnpy.trader.gateway import BaseGateway
from vnpy.trader.object import CancelRequest, OrderRequest, SubscribeRequest, TickData, HistoryRequest, BarData


class BackTesterBarGateway(BaseGateway):
    """
    回测网关： 加载BAR数据，并广播
    """

    default_name: str = "BACKTESTER_BAR"

    def __init__(self, event_engine: EventEngine, gateway_name: str) -> None:
        super().__init__(event_engine, gateway_name)
        self.database: BaseDatabase = get_database()
        print(f'{self.database.get_tick_overview()}')

    def query_history(self, req: HistoryRequest) -> List[BarData]:
        data:list[BarData] = self.database.load_bar_data(symbol=req.symbol, exchange=req.exchange,
                                                   # start=datetime(2019, 7, 1), end=datetime(2024, 10, 1)
                                                   start=req.start, end=req.end, interval=req.interval
                                                   )
        # 广播
        for bar in data:
            event: Event = Event(EVENT_BAR, bar)
            self.event_engine.put(event)

        None

    def subscribe(self, req: SubscribeRequest) -> None:
        pass

    def connect(self, setting: dict) -> None:
        pass

    def close(self) -> None:
        pass

    def send_order(self, req: OrderRequest) -> str:
        pass

    def cancel_order(self, req: CancelRequest) -> None:
        pass

    def query_account(self) -> None:
        pass

    def query_position(self) -> None:
        pass

